﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Lenovo.CFI.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
namespace Lenovo.CFI.DAL
{
    /// <summary>
    /// should remove
    /// </summary>
    public class TopicDcIpDa
    {
        public static void InsertTopicDcIp(TopicDcIp topicDcIp)
        {
            InsertTopicDcIp(topicDcIp, DBHelper.GetDatabase(), null);
        }
        public static void InsertTopicDcIp(TopicDcIp topicDcIp, TranscationHelper trans)
        {
            InsertTopicDcIp(topicDcIp, trans.DataBase, trans.Transaction);
        }
        private static void InsertTopicDcIp(TopicDcIp topicDcIp, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_InsertTopicDcIp", topicDcIp.TopicID, topicDcIp.User,
                                                          topicDcIp.Aspect);

            try
            {
                if (trans == null)
                    topicDcIp.ID = Convert.ToInt32(db.ExecuteScalar(dbCommand));
                else
                    topicDcIp.ID = Convert.ToInt32(db.ExecuteScalar(dbCommand, trans));
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void UpdateTopicDcIp(TopicDcIp topicDcIp)
        {
            UpdateTopicDcIp(topicDcIp, DBHelper.GetDatabase(), null);
        }
        public static void UpdateTopicDcIp(TopicDcIp topicDcIp, TranscationHelper trans)
        {
            UpdateTopicDcIp(topicDcIp, trans.DataBase, trans.Transaction);
        }
        private static void UpdateTopicDcIp(TopicDcIp topicDcIp, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_UpdateTopicDcIp", topicDcIp.ID, topicDcIp.TopicID,
                                                          topicDcIp.User, topicDcIp.Aspect);


            try
            {
                if (trans == null)
                    db.ExecuteNonQuery(dbCommand);
                else
                    db.ExecuteNonQuery(dbCommand, trans);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, false);
            }
        }

        public static void DeleteTopicDcIp(int id)
        {
            DeleteTopicDcIp(id, DBHelper.GetDatabase(), null);
        }
        public static void DeleteTopicDcIp(int id, TranscationHelper trans)
        {
            DeleteTopicDcIp(id, trans.DataBase, trans.Transaction);
        }
        private static void DeleteTopicDcIp(int id, Database db, DbTransaction trans)
        {
            DbCommand dbCommand = db.GetStoredProcCommand("Usp_DeleteTopicDcIp", id);

            try
            {
                if (trans == null)
                    db.ExecuteNonQuery(dbCommand);
                else
                    db.ExecuteNonQuery(dbCommand, trans);
            }
            catch (System.Data.SqlClient.SqlException sex)      // 只捕获SqlException，其余抛出继续传播
            {
                DBHelper.ParseSqlException(sex, true);
            }
        }

        public static List<TopicDcIp> GetTopicDcIpList(int topicID)
        {
            Database db = DBHelper.GetDatabase();

            DbCommand dbCommand = db.GetStoredProcCommand("Usp_GetTopicDcIpList", topicID);

            List<TopicDcIp> topicDcIps = new List<TopicDcIp>();
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                while (dataReader.Read())
                {
                    topicDcIps.Add(PopulateTopicDcIp(dataReader));
                }
            }

            return topicDcIps;
        }

        private static TopicDcIp PopulateTopicDcIp(IDataReader dataReader)
        {
            TopicDcIp topicDcIp = new TopicDcIp(dataReader.GetInt32(0), dataReader.GetInt32(1),
                                                       dataReader.GetString(2), dataReader.GetString(3));

            return topicDcIp;
        }
    }
}
